//
//  LoginViewController.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/3/30.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginAPIManager.h"
#import "LoginHistoryAccountView.h"
#import "SelectedSchoolController.h"
@interface LoginViewController ()<UITextFieldDelegate ,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *tableHeaderView;

@property(nonatomic ,strong)UIView *tableFootorView;

@property(nonatomic ,strong)UIView *lineView;

@property(nonatomic ,strong)UIButton *loginTypeBtn;

@property(nonatomic ,strong)YYLabel *protocolLabel;

@property(nonatomic ,strong)UIButton *protocolSelectButton;

@property(nonatomic ,strong)UIButton *loginButton;

@property(nonatomic ,strong)UITextField *accountTextFiled;

@property(nonatomic ,strong)UITextField *passwordTextFiled;

@property(nonatomic ,strong)UITextField *schoolNameTextFiled;

@property(nonatomic ,strong)UIButton *schoolSelectedBtn;

@property(nonatomic ,strong)UITextField *userNameTextFiled;

@property(nonatomic ,strong)UITextField *veriftyTextFiled;

@property(nonatomic ,strong)UIButton *forgetButton;

@property(nonatomic ,strong)WKWebView *captchaWeb;

@property(nonatomic ,strong)NSMutableURLRequest *captchaRequest;

@property(nonatomic ,copy)NSString *captcha_token;

@property(nonatomic ,strong)LoginHistoryAccountView *countView;

@property(nonatomic ,strong)UIImageView *headerBgImageView;

@property(nonatomic ,strong)NSString *school_id;

@end

@implementation LoginViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}



-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    [self.view addSubview:self.headerBgImageView];
    [self.view addSubview:self.tableView];
    [self.tableView setCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadius:16];
    
    
    [BaseRequest checkoutNetWorkStatus:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWiFi || status == AFNetworkReachabilityStatusReachableViaWWAN) {
            [self.captchaWeb loadRequest:self.captchaRequest];
        }
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0,0,lScreenWidth,16);
    label.numberOfLines = 0;
    label.text = @"全国服务热线（400-0755-330）";
    label.textColor = rgba(181, 181, 181, 1);
    label.font = RFontWithSize(12);
    [self.view addSubview:label];
    label.textAlignment = NSTextAlignmentCenter;
    label.y = lScreenHeight - SafeAreaBottomHeight - label.height - 10;
    
    
}
#pragma mark -API
-(void)doLogin
{
    NSMutableDictionary *dic = @{@"captcha":self.veriftyTextFiled.text,@"captcha_token":self.captcha_token,@"password":self.passwordTextFiled.text}.mutableCopy;
    
    if(self.loginTypeBtn.tag == 1)
    {
        [dic setObject:self.accountTextFiled.text forKey:@"account"];
    }
    if(self.loginTypeBtn.tag == 2)
    {
        [dic setObject:self.userNameTextFiled.text forKey:@"account"];
        [dic setObject:self.school_id forKey:@"school_id"];
    }
    [dic setObject:@"2" forKey:@"type"];
    
    [LoginAPIManager LoginWithParam:dic result:^(NSDictionary * _Nonnull dic) {
        if([dic[@"code"] intValue] == 0)
        {
            [self reloadWeb];
            [SVProgressHUD showErrorWithStatus:dic[@"msg"]];
        }
        if([dic[@"code"] intValue] == 1)
        {
            if(self.loginTypeBtn.tag == 1)
            {
                NSDictionary *dic = @{@"account":self.accountTextFiled.text,@"password":self.passwordTextFiled.text};
                [LoginHistoryAccountView saveHistoryAccountWithAcount:@[dic]];
            }
          
            [lApp goHome];
            if(dic[@"data"][@"userinfo"])
                [[NSUserDefaults standardUserDefaults] setObject:[dic[@"data"][@"userinfo"] modelToJSONString] forKey:USER_LOGIN_INFO];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        //
    }];
}

#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];
    if(self.loginTypeBtn.tag == 1)
    {
        if(indexPath.row == 0)
        {
            [cell.contentView addSubview:self.accountTextFiled];
        }else if (indexPath.row == 1)
        {
            [cell.contentView addSubview:self.passwordTextFiled];
        }else{
            [cell.contentView addSubview:self.veriftyTextFiled];
        }
        
    }else{
        if(indexPath.row == 0)
        {
            [cell.contentView addSubview:self.schoolNameTextFiled];
            [cell.contentView addSubview:self.schoolSelectedBtn];
        }else if (indexPath.row == 1)
        {
            [cell.contentView addSubview:self.userNameTextFiled];
        }else if (indexPath.row == 2)
        {
            [cell.contentView addSubview:self.passwordTextFiled];
        }else{
            [cell.contentView addSubview:self.veriftyTextFiled];
        }
    }
    
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if(self.loginTypeBtn.tag == 2)
    {
        return 4;
    }
    return 3;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 65;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.loginTypeBtn.tag == 2)
    {
        if(indexPath.row == 0){
            [self goingSelectSchoolAction];
        }
    }
    
}



#pragma mark -UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    //[[textField viewWithTag:1001] setBackgroundColor:kOrangeLineColor];
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    //[[textField viewWithTag:1001] setBackgroundColor:kLineColor];
    
    return  YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}//https://d-xueer.bigchun.com/h5test/#/followReading/handHomework
#pragma mark - private
- (void)goProtocolDetailWithKind:(int)kind
{
    UIViewController *vc = [[UIViewController alloc] initWithClassName:@"LProtocolShowViewController"];
    vc.paraObject = [NSNumber numberWithInt:kind];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)changeLoginBtnStatue
{
    BOOL canLogin = NO;
    if(self.loginTypeBtn.tag == 1)
    {
        if(self.protocolSelectButton.selected&&self.accountTextFiled.text.length > 0&&self.passwordTextFiled.text.length > 0)
        {
            canLogin = YES;
        }
    }else{
        
        if(self.protocolSelectButton.selected&&self.userNameTextFiled.text.length > 0&&self.passwordTextFiled.text.length > 0&&self.schoolNameTextFiled.text.length > 0)
        {
            canLogin = YES;
        }
        
    }
    
    
    if(canLogin)
    {
        self.loginButton.selected = YES;
        self.loginButton.backgroundColor = lClearColor;
    }else{
        self.loginButton.selected = NO;
        self.loginButton.backgroundColor = rgba(229, 229, 229, 1);
    }
    
}
#pragma mark -event response
-(void)changeLogin:(UIButton *)btn{
    
    [self.view endEditing:YES];
    if(self.loginTypeBtn!=btn)
    {
        self.loginTypeBtn = btn;
        [UIView animateWithDuration:0.3 animations:^{
            self.lineView.centerX = self.loginTypeBtn.centerX;
        }];
    }
    
}
- (void)tapClick
{
    [self.view endEditing:YES];
}
- (void)goFindingPWD
{
    [self.view endEditing:YES];
    [self pushToViewControllerWithClassName:@"FundPwdViewController" animated:YES];
}
- (void)loginButtonAction
{
    [self.view endEditing:YES];
    if(self.loginButton.selected)
    {
        
        [MethodTool getCookieWithwebView:self.captchaWeb success:^(BOOL success) {
            [self doLogin];
        }];
        
    }
}
-(void)goingSelectSchoolAction
{
    [self.view endEditing:YES];
    
    SelectedSchoolController *vc = [[SelectedSchoolController alloc] initWithClassName:@"SelectedSchoolController"];
    vc.block = ^(NSDictionary * _Nonnull school) {
        self.schoolNameTextFiled.text = school[@"name"];
        self.school_id = school[@"id"];
    };
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)reloadWeb
{
    [self.view endEditing:YES];
    [self.captchaWeb loadRequest:self.captchaRequest];
}

-(void)showMoreAcountAction:(UIButton *)sender
{
    [self.view endEditing:YES];
    sender.selected =!sender.selected;
    
    
    if(self.countView.superview)
    {
        [self.countView removeFromSuperview];
    }else{
        [self.view addSubview:self.countView];
        [self.countView show];
    }
    
    //[self.captchaWeb loadRequest:self.captchaRequest];
}
-(void)protocolSelectButtonAction:(UIButton *)sender
{
    sender.selected =!sender.selected;
    [self changeLoginBtnStatue];
    //    if ([NSString isMatchText:self.phoneTextField.text withPattern:RegexMobileNumber]&&self.protocolSelectButton.selected)
    //    {
    //        self.registerButton.enabled =YES;
    //    }
    //    else
    //    {
    //        self.registerButton.enabled =NO;
    //    }
}

-(void)textFieldResignTapAction:(UITapGestureRecognizer *)sender
{
    [self.view endEditing:YES];
}
-(void)textEnd:(UITextField *)textField
{
    [self.tableView reloadData];
}
-(void)textChanged:(UITextField *)textField
{
    
    [self changeLoginBtnStatue];
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(10, 88, lScreenWidth - 20, lScreenHeight - 88) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:lWhiteColor];
        [_tableView setSeparatorColor:lClearColor];
        [_tableView setTableHeaderView:self.tableHeaderView];
        [_tableView setTableFooterView:self.tableFootorView];
    }
    
    return _tableView;
}

-(UIView *)tableHeaderView
{
    if (!_tableHeaderView)
    {
        
        _tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth,210)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [_tableHeaderView addGestureRecognizer:tap];
        UIView *viewRound = [[UIView alloc] init];
        viewRound.frame = CGRectMake(155,128,65,65);
        viewRound.center = CGPointMake(lScreenWidth/2.0, 72.5);
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,65,65);
        gl.startPoint = CGPointMake(0.5, 0);
        gl.endPoint = CGPointMake(0.5, 1);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        viewRound.layer.cornerRadius = 32.5;
        viewRound.layer.masksToBounds = YES;
        [viewRound.layer addSublayer:gl];
        [_tableHeaderView addSubview:viewRound];
        //RFontWithSize
        UIButton *fastLBtn = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(30, viewRound.bottom + 40, (lScreenWidth - 60 - 15)/2.0, 28);
            [button setTitle:@"快捷登录" forState:UIControlStateNormal];
            button.titleLabel.font = MFontWithSize(20.f);
            [button setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateSelected];
            [button setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
            [button addTarget:self action:@selector(changeLogin:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
        fastLBtn.tag = 1;
        
        UIButton *nameLBtn = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(fastLBtn.right + 15, viewRound.bottom + 40, (lScreenWidth - 60 - 15)/2.0, 28);
            [button setTitle:@"实名登录" forState:UIControlStateNormal];
            button.titleLabel.font = MFontWithSize(20.f);
            [button setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateSelected];
            [button setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
            [button addTarget:self action:@selector(changeLogin:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
        nameLBtn.tag = 2;
        self.loginTypeBtn = fastLBtn;
        [_tableHeaderView addSubview:fastLBtn];
        [_tableHeaderView addSubview:nameLBtn];
        [_tableHeaderView addSubview:self.lineView];
        _lineView.y = fastLBtn.bottom + 5;
        _lineView.centerX = fastLBtn.centerX;
        [_tableHeaderView setHeight:self.lineView.bottom + 20];
        
    }
    
    return _tableHeaderView;
}

-(UIView *)tableFootorView
{
    if (!_tableFootorView)
    {
        _tableFootorView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth - 20, 20)];
        //        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        //        [_tableFootorView addGestureRecognizer:tap];
        UIView *loginView =[[UIView alloc] initWithFrame:CGRectMake(20, 65, lScreenWidth - 60, 45)];
        [loginView setAllCornersWithCornerRadius:22.5];
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,lScreenWidth - 60,45);
        gl.startPoint = CGPointMake(1, 0.5);
        gl.endPoint = CGPointMake(0, 0.5);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [loginView.layer addSublayer:gl];
        [loginView addSubview:self.loginButton];
        [_tableFootorView addSubview:self.forgetButton];
        [_tableFootorView addSubview:loginView];
        [_tableFootorView addSubview:self.protocolSelectButton];
        [_tableFootorView addSubview:self.protocolLabel];
        [self.protocolSelectButton setY:loginView.bottom + 5];
        [self.protocolLabel setY:loginView.bottom + 15];
        _tableFootorView.height = self.protocolLabel.bottom;
    }
    
    return _tableFootorView;
}

- (UIImageView *)headerBgImageView
{
    if(!_headerBgImageView)
    {
        _headerBgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, 313.0/375*lScreenWidth)];
        _headerBgImageView.image = IMAGENAMED(@"ic_login_header_bg");
        
    }
    return _headerBgImageView;
}
- (UIView *)lineView
{
    if(!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.frame = CGRectMake(95,266,30,3);
        _lineView.backgroundColor = rgba(12, 183, 108, 1);
        [_lineView setAllCornersWithCornerRadius:1.5];
    }
    return _lineView;
}
-(UIButton *)loginButton
{
    if (!_loginButton)
    {
        _loginButton = [UIButton new];
        _loginButton.frame =CGRectMake(0, 0, lScreenWidth - 60, 45);
        
        _loginButton.selected =NO;
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:rgba(181, 181, 181, 1) forState:UIControlStateNormal];
        [_loginButton setTitleColor:lWhiteColor forState:UIControlStateSelected];
        [_loginButton.titleLabel setFont:MFontWithSize(18)];
        [_loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:UIControlEventTouchUpInside];
        _loginButton.backgroundColor = rgba(229, 229, 229, 1);
        
    }
    
    return _loginButton;
}
-(UIButton *)forgetButton
{
    if (!_forgetButton)
    {
        _forgetButton = [UIButton new];
        _forgetButton.frame =CGRectMake(lScreenWidth - 100, 5, 70, 20);
        
        [_forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
        [_forgetButton setTitleColor:rgba(142, 142, 142, 1) forState:UIControlStateNormal];
        [_forgetButton.titleLabel setFont:RFontWithSize(14)];
        [_forgetButton addTarget:self action:@selector(goFindingPWD) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _forgetButton;
}

-(UIButton *)protocolSelectButton
{
    if (!_protocolSelectButton)
    {
        _protocolSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_protocolSelectButton setImage:[UIImage imageNamed:@"ic_protocol_unSelect"] forState:UIControlStateNormal];
        [_protocolSelectButton setImage:[UIImage imageNamed:@"ic_protocol_select"] forState:UIControlStateSelected];
        [_protocolSelectButton addTarget:self action:@selector(protocolSelectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //        _protocolSelectButton.imageEdgeInsets =UIEdgeInsetsMake(15, 0, 15, 10);
        //_protocolSelectButton.imageEdgeInsets =UIEdgeInsetsMake(12.5, 0, 12.5, 5);
        _protocolSelectButton.frame = CGRectMake(15, 5, 26, 40);
        _protocolSelectButton.selected =NO;
    }
    
    return _protocolSelectButton;
}
-(YYLabel *)protocolLabel
{
    if (!_protocolLabel)
    {
        NSMutableAttributedString *text = [NSMutableAttributedString new];
        NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"同意"];
        one.color = rgba(181, 181, 181, 1);
        NSMutableAttributedString *two = [[NSMutableAttributedString alloc] initWithString:@"《使用协议》、"];
        NSMutableAttributedString *three = [[NSMutableAttributedString alloc] initWithString:@"《隐私政策》、"];
        NSMutableAttributedString *four = [[NSMutableAttributedString alloc] initWithString:@"《儿童隐私保护政策》"];
        
        two.font = one.font = three.font = four.font = MFontWithSize(12);
        
        [two setTextHighlightRange:two.rangeOfAll
                             color:rgba(12, 183, 108, 1)
                   backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]
                         tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)
         {
            [self goProtocolDetailWithKind:1];
        }];
        
        [three setTextHighlightRange:three.rangeOfAll
                               color:rgba(12, 183, 108, 1)
                     backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]
                           tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)
         {
            [self goProtocolDetailWithKind:2];
        }];
        
        [four setTextHighlightRange:four.rangeOfAll
                              color:rgba(12, 183, 108, 1)
                    backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]
                          tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)
         {
            [self goProtocolDetailWithKind:3];
        }];
        
        [text appendAttributedString:one];
        [text appendAttributedString:two];
        [text appendAttributedString:three];
        [text appendAttributedString:four];
        
        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 5.0;
        [text addAttribute:NSParagraphStyleAttributeName
                     value:paragraphStyle
                     range:NSMakeRange(0, text.string.length)];
        
        YYLabel *label = [YYLabel new];
        label.numberOfLines =0;
        label.textAlignment =NSTextAlignmentCenter;
        label.attributedText =text;
        label.frame =CGRectMake(self.protocolSelectButton.right + 3, 15, lScreenWidth - 35 - self.protocolSelectButton.right - 3, 21);
        [label sizeToFit];
        _protocolLabel =label;
    }
    
    return _protocolLabel;
}

-(UITextField *)schoolNameTextFiled
{
    if (!_schoolNameTextFiled)
    {
        _schoolNameTextFiled =[[UITextField alloc] initWithFrame:CGRectMake(20, 10, lScreenWidth - 60, 45)];
        _schoolNameTextFiled.enabled = NO;
        _schoolNameTextFiled.font = MFontWithSize(16);
        _schoolNameTextFiled.textColor = rgba(51, 51, 51, 1);
        _schoolNameTextFiled.clearButtonMode =UITextFieldViewModeWhileEditing;
        _schoolNameTextFiled.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:@"请选择所在学校" attributes:@{NSFontAttributeName:MFontWithSize(16)}];
        _schoolNameTextFiled.delegate =self;
        if (@available(iOS 11.0,*)) {
            _schoolNameTextFiled.textContentType =UITextContentTypeUsername;
        }
        [_schoolNameTextFiled setAllCornersWithCornerRadius:22.5];
        _schoolNameTextFiled.backgroundColor = rgba(245, 245, 245, 1);
        [_schoolNameTextFiled addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEnd];
        [_schoolNameTextFiled addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        UIView *leftSpaceView = [[UIView alloc] init];
        leftSpaceView.frame = CGRectMake(0,0,15,45);
        leftSpaceView.backgroundColor =rgba(245, 245, 245, 1);
        _schoolNameTextFiled.leftViewMode = UITextFieldViewModeAlways;
        _schoolNameTextFiled.leftView = leftSpaceView;
        UIView *rightSpaceView = [[UIView alloc] init];
        rightSpaceView.frame = CGRectMake(0,0,44,45);
        rightSpaceView.backgroundColor =rgba(245, 245, 245, 1);
        
    }
    
    return _schoolNameTextFiled;
}
- (UIButton *)schoolSelectedBtn
{
    if(!_schoolSelectedBtn)
    {
        _schoolSelectedBtn = [UIButton new];
        _schoolSelectedBtn.frame =CGRectMake(self.schoolNameTextFiled.right - 44, self.schoolNameTextFiled.y, 44, 45);
        _schoolSelectedBtn.backgroundColor = lClearColor;
        [_schoolSelectedBtn setImage:IMAGENAMED(@"ic_arrow_right") forState:UIControlStateNormal];
        [_schoolSelectedBtn addTarget:self action:@selector(goingSelectSchoolAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _schoolSelectedBtn;
}
-(UITextField *)userNameTextFiled
{
    if (!_userNameTextFiled)
    {
        _userNameTextFiled =[[UITextField alloc] initWithFrame:CGRectMake(20, 10, lScreenWidth - 60, 45)];
        _userNameTextFiled.font = MFontWithSize(16);
        _userNameTextFiled.textColor = rgba(51, 51, 51, 1);
        _userNameTextFiled.clearButtonMode =UITextFieldViewModeWhileEditing;
        _userNameTextFiled.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:@"请输入中文姓名" attributes:@{NSFontAttributeName:MFontWithSize(16)}];
        _userNameTextFiled.delegate = self;
        if (@available(iOS 11.0,*)) {
            _userNameTextFiled.textContentType =UITextContentTypeUsername;
        }
        [_userNameTextFiled setAllCornersWithCornerRadius:22.5];
        _userNameTextFiled.backgroundColor = rgba(245, 245, 245, 1);
        [_userNameTextFiled addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        [_userNameTextFiled addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEnd];
        UIView *leftSpaceView = [[UIView alloc] init];
        leftSpaceView.frame = CGRectMake(0,0,15,45);
        leftSpaceView.backgroundColor =rgba(245, 245, 245, 1);
        _userNameTextFiled.leftViewMode = UITextFieldViewModeAlways;
        _userNameTextFiled.leftView = leftSpaceView;
        
    }
    
    return _userNameTextFiled;
}
-(UITextField *)veriftyTextFiled
{
    if (!_veriftyTextFiled)
    {
        _veriftyTextFiled =[[UITextField alloc] initWithFrame:CGRectMake(20, 10, lScreenWidth - 60, 45)];
        _veriftyTextFiled.font = MFontWithSize(16);
        _veriftyTextFiled.textColor = rgba(51, 51, 51, 1);
        _veriftyTextFiled.clearButtonMode =UITextFieldViewModeWhileEditing;
        _veriftyTextFiled.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSFontAttributeName:MFontWithSize(16)}];
        _veriftyTextFiled.delegate =self;
        
        if (@available(iOS 11.0,*)) {
            _veriftyTextFiled.textContentType =UITextContentTypeUsername;
        }
        [_veriftyTextFiled setAllCornersWithCornerRadius:22.5];
        _veriftyTextFiled.backgroundColor = rgba(245, 245, 245, 1);
        [_veriftyTextFiled addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        UIView *leftSpaceView = [[UIView alloc] init];
        leftSpaceView.frame = CGRectMake(0,0,15,45);
        leftSpaceView.backgroundColor =rgba(245, 245, 245, 1);
        _veriftyTextFiled.leftViewMode = UITextFieldViewModeAlways;
        _veriftyTextFiled.leftView = leftSpaceView;
        UIView *rightSpaceView = [[UIView alloc] init];
        rightSpaceView.frame = CGRectMake(0,0,100,45);
        rightSpaceView.backgroundColor =rgba(245, 245, 245, 1);
        [rightSpaceView addSubview:self.captchaWeb];
        [self.captchaWeb loadRequest:self.captchaRequest];
        [rightSpaceView addSubview:self.captchaWeb];
        UIButton *relodBtn = [UIButton new];
        relodBtn.frame = CGRectMake(0,0,100,45);;
        
        [rightSpaceView addSubview:relodBtn];
        [relodBtn addTarget:self action:@selector(reloadWeb) forControlEvents:UIControlEventTouchUpInside];
        _veriftyTextFiled.rightViewMode = UITextFieldViewModeAlways;
        _veriftyTextFiled.rightView = rightSpaceView;
        
    }
    
    return _veriftyTextFiled;
}
- (WKWebView *)captchaWeb
{
    if(!_captchaWeb)
    {
        _captchaWeb = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, 100, 45)];
        
    }
    return _captchaWeb;
}
- (NSMutableURLRequest *)captchaRequest
{
     self.captcha_token =  [NSString stringWithFormat:@"%d",arc4random()];
    NSString *url = [NSString stringWithFormat:@"%@%@%@%@",BaseUrl,KM_API_User_Login_Captcha_Url,@"?id=",self.captcha_token];
    return [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
}
-(UITextField *)accountTextFiled
{
    if (!_accountTextFiled)
    {
        _accountTextFiled =[[UITextField alloc] initWithFrame:CGRectMake(20, 10, lScreenWidth - 60, 45)];
        _accountTextFiled.font = MFontWithSize(16);
        _accountTextFiled.textColor = rgba(51, 51, 51, 1);
        _accountTextFiled.clearButtonMode =UITextFieldViewModeWhileEditing;
        _accountTextFiled.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:@"请输入账号" attributes:@{NSFontAttributeName:MFontWithSize(16)}];
        _accountTextFiled.delegate =self;
        // [_accountTextFiled setInputAccessoryView:self.customTextFieldInputAccessoryView];
        if (@available(iOS 11.0,*)) {
            _accountTextFiled.textContentType =UITextContentTypeUsername;
        }
        [_accountTextFiled setAllCornersWithCornerRadius:22.5];
        _accountTextFiled.backgroundColor = rgba(245, 245, 245, 1);
        [_accountTextFiled addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        [_accountTextFiled addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEnd];
        UIView *leftSpaceView = [[UIView alloc] init];
        leftSpaceView.frame = CGRectMake(0,0,15,45);
        leftSpaceView.backgroundColor =rgba(245, 245, 245, 1);
        _accountTextFiled.leftViewMode = UITextFieldViewModeAlways;
        _accountTextFiled.leftView = leftSpaceView;
        if([LoginHistoryAccountView getHistoryAccounts].count > 0)
        {
            UIView *rightSpaceView = [[UIView alloc] init];
            rightSpaceView.frame = CGRectMake(0,0,44,45);
            rightSpaceView.backgroundColor =rgba(245, 245, 245, 1);
            UIButton *showAccountBtn = [UIButton new];
            showAccountBtn.frame =CGRectMake(0, 0, 44, 45);
            
            [showAccountBtn setImage:IMAGENAMED(@"ic_arrow_down") forState:UIControlStateNormal];
            [showAccountBtn setImage:IMAGENAMED(@"ic_arrow_up") forState:UIControlStateSelected];
            [rightSpaceView addSubview:showAccountBtn];
            [showAccountBtn addTarget:self action:@selector(showMoreAcountAction:) forControlEvents:UIControlEventTouchUpInside];
            _accountTextFiled.rightViewMode = UITextFieldViewModeAlways;
            _accountTextFiled.rightView = rightSpaceView;
        }
    }
    
    return _accountTextFiled;
}
-(UITextField *)passwordTextFiled
{
    if (!_passwordTextFiled)
    {
        _passwordTextFiled =[[UITextField alloc] initWithFrame:CGRectMake(20, 10, lScreenWidth - 60, 45)];
        _passwordTextFiled.font = MFontWithSize(16);
        _passwordTextFiled.textColor = rgba(51, 51, 51, 1);
        _passwordTextFiled.clearButtonMode =UITextFieldViewModeWhileEditing;
        _passwordTextFiled.secureTextEntry =YES;
        _passwordTextFiled.delegate =self;
        _passwordTextFiled.placeholder = @"请输入密码";
        [_passwordTextFiled setAllCornersWithCornerRadius:22.5];
        _passwordTextFiled.backgroundColor = rgba(245, 245, 245, 1);
        //[_passwordTextFiled setInputAccessoryView:self.customTextFieldInputAccessoryView];
        
        
        if (@available(iOS 11.0,*)) {
            _passwordTextFiled.textContentType =UITextContentTypePassword;
        }
        
        [_passwordTextFiled addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        [_passwordTextFiled addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEnd];
        UIView *leftSpaceView = [[UIView alloc] init];
        leftSpaceView.frame = CGRectMake(0,0,15,45);
        leftSpaceView.backgroundColor =rgba(245, 245, 245, 1);
        _passwordTextFiled.leftViewMode = UITextFieldViewModeAlways;
        _passwordTextFiled.leftView = leftSpaceView;
    }
    
    return _passwordTextFiled;
}
- (LoginHistoryAccountView *)countView
{
    if(!_countView)
    {
        _countView = [[LoginHistoryAccountView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, lScreenHeight)];
        __weak typeof(self) weakSelf = self;
        _countView.block = ^(NSDictionary * _Nonnull accountInfo) {
            weakSelf.accountTextFiled.text = accountInfo[@"account"];
            weakSelf.passwordTextFiled.text = accountInfo[@"password"];
        };
        
    }
    return _countView;
}
- (void)setLoginTypeBtn:(UIButton *)loginTypeBtn
{
    _loginTypeBtn.selected = NO;
    _loginTypeBtn = loginTypeBtn;
    _loginTypeBtn.selected = YES;
    self.passwordTextFiled.text = @"";
    self.veriftyTextFiled  = nil;
    [self.tableView reloadData];
    
}
@end
